"""
寻找query中的最短匹配
query = A B C D E  每个是一个term
doc里面，经常会有XBXXXXXA XEXAXXX C XXXX DDXXXB]XXXXCXXXXDXXXAXXXXEXXXE
query和doc匹配里，至少命中一次，包含A B C D E的doc的最小长度是多少；（ABCDE不考虑顺序）
input
str1 = ABCDE
str2 = AXXBXXACXXDEXBXXAXXXCXXXXDXXXE
"""


def func(str1: str, str2: str):
    query = {}
    for s in str1:
        query[s] = []
    for i in range(len(str2)):
        if str2[i] in query:
            query[str2[i]].append(i)
    query_list = []
    for key, value in query.items():
        query_list.append(value)

    result = [y[0] for y in query_list]
    min_len = max(result) - min(result) + 1
    pointer = [1] * len(query_list)
    target = [len(x) for x in query_list]
    while True:
        min_index = result.index(min(result))
        if pointer[min_index] == target[min_index]:
            break
        result[min_index] = query_list[min_index][pointer[min_index]]
        pointer[min_index] += 1
        if (max(result) - min(result) + 1) < min_len:
            min_len = max(result) - min(result) + 1
    return min_len


if __name__ == '__main__':
    length = func('ABCDE', 'AXXBXXACXXDEXBXXAXXXCXXXXDXXXE')
    print(length)

